using System.Collections.Generic;
using System.IO;
using Gameplay.PVE;
using Gameplay.PVE.Editor;
using UnityEditor;
using UnityEngine;

[CustomEditor( typeof(AnimatorPlayer))]
public  class EditAnimatorInspector : Editor
{
     /// <summary>
     /// 滑动杆的当前时间
     /// </summary>
     private  float m_CurTime;
     
     /// <summary>
     /// 当前是否是预览播放状态
     /// </summary>
     private  bool m_Playing;

     /// <summary>
     /// 当前运行时间
     /// </summary>
     private  float m_RunningTime;
    
     /// <summary>
     /// 上一次系统时间
     /// </summary>
     private  double m_PreviousTime;
    
     /// <summary>
     /// 总的记录时间
     /// </summary>
     private  float m_RecorderStopTime;

     /// <summary>
     /// 滑动杆总长度
     /// </summary>
     private  const  float kDuration = 7f;

     private Animator m_Animator;

     private AnimatorPlayer editAnimator { get {  return target  as AnimatorPlayer; } }

     private EditorAnimPlayer animPlayer;

     private Animator animator
    {
        get {  return m_Animator ?? (m_Animator = editAnimator.GetComponent<Animator>()); }
    }

     void OnEnable()
    {
        m_PreviousTime = EditorApplication.timeSinceStartup;
        EditorApplication.update += inspectorUpdate;
    }

     void OnDisable()
    {
        EditorApplication.update -= inspectorUpdate;
    }

     private string customActionName = "自定义动作";
     
     public  override  void OnInspectorGUI()
    {
        EditorGUILayout.BeginHorizontal();
        /*if (GUILayout.Button( "Init"))
        {
            editAnimator.Init();
        }
        if (GUILayout.Button( "Stop"))
        {
            m_Playing = false;
        }*/
        var actions = editAnimator.actions;
        if (actions != null)
        {
            EditorGUILayout.BeginVertical();
            for (int i = 0; i < actions.Count; i++)
            {
                if (GUILayout.Button(actions[i]))
                {
                    if (animPlayer == null)
                    {
                        animPlayer = new EditorAnimPlayer();
                        animPlayer.Initialize(editAnimator.gameObject.GetComponentInChildren<Animator>());
                    }
                    m_Playing = false;
                    m_CurTime = 0;
                    m_PreviousTime = EditorApplication.timeSinceStartup;
                    animPlayer.Play(actions[i]);
                }
            }

            customActionName = GUILayout.TextField(customActionName);
            if (GUILayout.Button(customActionName))
            {
                if (animPlayer == null)
                {
                    animPlayer = new EditorAnimPlayer();
                    animPlayer.Initialize(editAnimator.gameObject.GetComponentInChildren<Animator>());
                }
                m_Playing = true;
                m_CurTime = 0;
                m_PreviousTime = EditorApplication.timeSinceStartup;
                animPlayer.Play(customActionName);
            }

            if (GUILayout.Button("还原到基础动作"))
            {
                m_Playing = false;
                editAnimator.ChangeModelToDefault(editAnimator.gameObject);
            }
            
            EditorGUILayout.EndVertical();
        }
        
        EditorGUILayout.EndHorizontal();
        m_CurFrame = (int)EditorGUILayout.Slider( "Frame:", m_CurFrame, 0f, kDuration * 30);
        m_CurTime = m_CurFrame / 30f;
        manualUpdate();
    }
    

     private int m_CurFrame = 0;

     /// <summary>
     /// 非预览播放状态下，通过滑杆来播放当前动画帧
     /// </summary>
     private  void manualUpdate()
    {
         if (animPlayer != null && !m_Playing)
        {
            animPlayer.Update(m_CurTime);
        }
    }

     private  void inspectorUpdate()
    {
        var delta = EditorApplication.timeSinceStartup - m_PreviousTime;
        m_PreviousTime = EditorApplication.timeSinceStartup;
        if (!Application.isPlaying && m_Playing)
        {
            m_CurTime += (float) delta;
            animPlayer.Update(delta);
        }
    }
}